//
//  WantViewController.m
//  觅影
//
//  Created by  陶虹 on 16/4/12.
//  Created by lanou on 16/4/13.
//  Copyright © 2016年  陶虹. All rights reserved.
//

#import "WantViewController.h"
#import "FilmIntroModel.h"
#import "WantTableViewCell.h"
#import "LibraryDetailViewController.h"

@interface WantViewController () <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong)NSMutableArray *modelArray;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong)FMDatabase *db;

@end

@implementation WantViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.title = @"想看";
    
    // 获取数据库路径
    NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)firstObject]stringByAppendingPathComponent:@"LibraryDetail.db"];
    self.db = [FMDatabase databaseWithPath:path];
    if (![self.db open]) {
        NSLog(@"不能打开数据库");
    }

    // 从数据库中抓取数据
    self.modelArray = [NSMutableArray array];
    FMResultSet *rs = [self.db executeQuery:@"SELECT * FROM WantFilm"];
    while ([rs next]) {
        NSString *poster = [rs stringForColumn:@"Poster"];
        NSString *title = [rs stringForColumn:@"Title"];
        NSString *filmStyle = [rs stringForColumn:@"FilmStyle"];
        NSString *filmId = [rs stringForColumn:@"FilmId"];
        FilmIntroModel *model = [[FilmIntroModel alloc]init];
        model.poster = poster;
        model.title = title;
        model.filmtype = filmStyle;
        model.filmId = filmId;
        [self.modelArray addObject:model];
    }
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 110;
    [self.tableView registerNib:[UINib nibWithNibName:@"WantTableViewCell" bundle:nil] forCellReuseIdentifier:@"WantCell"];
    if (self.modelArray.count == 0) {
        self.tableView.hidden = YES;
    }
    else
    {
        self.tableView.hidden = NO;
    }
    
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.modelArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WantTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WantCell" forIndexPath:indexPath];
    FilmIntroModel *model = self.modelArray[indexPath.row];
    cell.model = model;
    [cell.removeButton addTarget:self action:@selector(removeButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
    
}


- (void)removeButtonAction:(UIButton *)button
{
    // 根据点击的button找到cell
    WantTableViewCell *cell = (WantTableViewCell *)button.superview.superview;
        // 从数据库中移除数据
    NSString *movieId = cell.model.filmId;
    [self.db open];
    [self.db executeUpdate:@"DELETE FROM WantFilm WHERE FilmId = ?",movieId];
    [self.db close];
    // 根据cell取到indexPath
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    [self.modelArray removeObjectAtIndex:indexPath.row];
    [self.tableView reloadData];
    if (self.modelArray.count == 0) {
        self.tableView.hidden = YES;
    }
    else
    {
        self.tableView.hidden = NO;
    }
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    LibraryDetailViewController *libraryDetailVC = [[LibraryDetailViewController alloc]init];
    FilmIntroModel *model = self.modelArray[indexPath.row];
    libraryDetailVC.movieId = model.filmId;
    [self.navigationController pushViewController:libraryDetailVC animated:YES];
}












- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
